package com.atguigu.statistics.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.statistics.cline.MemberCline;
import com.atguigu.statistics.entity.Daily;
import com.atguigu.statistics.mapper.DailyMapper;
import com.atguigu.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author yky
 * @since 2020-07-18
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    //远程调用
    @Autowired
    private MemberCline memberCline;

    @Override
    @Transactional
    public void insertResNum(String day) {
        //添加记录前先删除
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //获取返回人数
        R numByDay = memberCline.getNumByDay(day);
        Map<String, Object> data = numByDay.getData();
        Integer num = (Integer) data.get("num");

        //模拟数据
        int nextInt = RandomUtils.nextInt(100, 200);
        //封装
        Daily daily = new Daily();
        daily.setRegisterNum(num);
        daily.setDateCalculated(day);
        daily.setCourseNum(nextInt);//课程
        daily.setLoginNum(nextInt);//登陆书
        daily.setVideoViewNum(nextInt);

        int insert = baseMapper.insert(daily);
        if(insert==0){
            throw new GuliException(20001,"获取数据失败");
        }
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        //根据条件查数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //选择字段
        wrapper.select("date_calculated",type);
        List<Daily> dailies = baseMapper.selectList(wrapper);

        //封装
        List<String> dateList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (Daily d:dailies) {
            dateList.add(d.getDateCalculated());
            switch (type){
                case "register_num":
                    numDataList.add(d.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(d.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(d.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(d.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculated",dateList);
        map.put("numDataList",numDataList);
        return map;
    }
}
